﻿/*
 * Class  :  Roles
 * Author : Chung Thành Phước 
 * Create Date: 18/12/2014
 * Update date: 18-12-2014 by Chung Thành Phước 
 * Reference: System.Data,System.Linq
 */
using System.Data;
using System.Linq;
using System.Web;
using BLL;

namespace Roles
{
    public class SecurityServices
    {
        private static DataTable _dtUserPermiss;
        private static readonly BllUserPermisstion _clsUserPermiss = new BllUserPermisstion();
        public static bool HasPermisstion(string FunctionName, int RolesID, Enums.TypeaAudit audit)
        {
            _dtUserPermiss = _clsUserPermiss.GetUserPermisstion(string.Empty, int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()));
            if (_dtUserPermiss != null && _dtUserPermiss.Rows.Count > 0)
            {
                return (from DataRow drUserPermiss in _dtUserPermiss.Rows
                    where
                        (int) drUserPermiss["Group_Id"] == RolesID &&
                        drUserPermiss["ModulesParent_Name"].ToString() == FunctionName
                    select Untils.HasPermisstion(audit, (int) drUserPermiss["PermisstionNumber"])).FirstOrDefault();
            }
            return false;
        }
        public static bool HasPermisstionPageMain(string FunctionName, int RolesID, bool Is_Page)
        {
            _dtUserPermiss = _clsUserPermiss.GetUserPermisstion(string.Empty, int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()));
            if (_dtUserPermiss != null && _dtUserPermiss.Rows.Count > 0)
            {
                return
                    _dtUserPermiss.Rows.Cast<DataRow>()
                        .Any(
                            drUserPermiss =>
                                (int) drUserPermiss["Group_Id"] == RolesID &&
                                drUserPermiss["ModulesMain_Name"].ToString() == FunctionName &&
                                (bool)drUserPermiss["IsPageMain"] == Is_Page);
            }
            return false;
        }
        public static bool HasPermisstionPageParrent(string FunctionName, int RolesID, bool Is_Page)
        {
            _dtUserPermiss = _clsUserPermiss.GetUserPermisstion(string.Empty, int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()));
            if (_dtUserPermiss != null && _dtUserPermiss.Rows.Count > 0)
            {
                return
                    _dtUserPermiss.Rows.Cast<DataRow>()
                        .Any(
                            drUserPermiss =>
                                (int)drUserPermiss["Group_Id"] == RolesID &&
                                drUserPermiss["ModulesParent_Name"].ToString() == FunctionName &&
                                (bool)drUserPermiss["IsPageParrent"] == Is_Page);
            }
            return false;
        }
        public static void HasPermisstion_Btn(string moduleName, out bool btnAdd, out bool btnEdit, out bool btnDelete, out bool btnView, out bool btnCancel, out bool btnPrint, out bool btnReview)
        {
            _dtUserPermiss = _clsUserPermiss.GetUserPermisstion(string.Empty, int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()));
            if (_dtUserPermiss != null && _dtUserPermiss.Rows.Count > 0)
            {
                btnAdd = _dtUserPermiss.Rows.Cast<DataRow>().Any(drUserPermiss => (int)drUserPermiss["Group_Id"] == int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()) && drUserPermiss["ModulesParent_Name"].ToString() == moduleName && (bool)drUserPermiss["IsbtnAddNew"]);
                btnEdit = _dtUserPermiss.Rows.Cast<DataRow>().Any(drUserPermiss => (int)drUserPermiss["Group_Id"] == int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()) && drUserPermiss["ModulesParent_Name"].ToString() == moduleName && (bool)drUserPermiss["IsbtnEdit"]);
                btnDelete = _dtUserPermiss.Rows.Cast<DataRow>().Any(drUserPermiss => (int)drUserPermiss["Group_Id"] == int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()) && drUserPermiss["ModulesParent_Name"].ToString() == moduleName && (bool)drUserPermiss["IsbtnDelete"]);
                btnView = _dtUserPermiss.Rows.Cast<DataRow>().Any(drUserPermiss => (int)drUserPermiss["Group_Id"] == int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()) && drUserPermiss["ModulesParent_Name"].ToString() == moduleName && (bool)drUserPermiss["IsbtnView"]);
                btnCancel = _dtUserPermiss.Rows.Cast<DataRow>().Any(drUserPermiss => (int)drUserPermiss["Group_Id"] == int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()) && drUserPermiss["ModulesParent_Name"].ToString() == moduleName && (bool)drUserPermiss["IsbtnCancel"]);
                btnPrint = _dtUserPermiss.Rows.Cast<DataRow>().Any(drUserPermiss => (int)drUserPermiss["Group_Id"] == int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()) && drUserPermiss["ModulesParent_Name"].ToString() == moduleName && (bool)drUserPermiss["IsbtnPrint"]);
                btnReview = _dtUserPermiss.Rows.Cast<DataRow>().Any(drUserPermiss => (int)drUserPermiss["Group_Id"] == int.Parse(HttpContext.Current.Session["USer_Detail_Id"].ToString()) && drUserPermiss["ModulesParent_Name"].ToString() == moduleName && (bool)drUserPermiss["IsbtnReview"]);
            }
            else
            {
                btnAdd = false;
                btnEdit = false;
                btnDelete = false;
                btnView = false;
                btnCancel = false;
                btnPrint = false;
                btnReview = false;
            }
        }
    }
}